package com.zl.learning.io.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaolei on 2017/3/9.
 */
public class PackageRead {
    public static void main(String[] args) {
        List<String> classNames = getClassName("com.zl.learning.io.aio");
        for (String s : classNames) {
            System.out.println(s);
        }
    }

    public static List<String> getClassName(String packageName) {
        packageName = packageName.replace(".", File.separator);
        String filePath = ClassLoader.getSystemResource("").getPath() + packageName;
        return getClassName(filePath, null, packageName);
    }


    private static List<String> getClassName(String filePath, List<String> className, String packageName) {
        List<String> myClassName = new ArrayList<>();
        File file = new File(filePath);
        File[] childFiles = file.listFiles();
        assert childFiles != null;
        for (File childFile : childFiles) {
            if (childFile.isDirectory()) {
                myClassName.addAll(getClassName(childFile.getPath(), myClassName, packageName));
            } else {
                String childFilePath = childFile.getPath();
                childFilePath = childFilePath.substring(childFilePath.indexOf(packageName), childFilePath.lastIndexOf("."));
                childFilePath = childFilePath.replace(File.separator, ".");
                myClassName.add(childFilePath);
            }
        }
        return myClassName;
    }
}
